'use strict';

const Service = require('egg').Service;

class HeroService extends Service {
    // 查看数据
    async getHeroList() {
        let heroList = await this.app.model.Hero.findAll({});
        return heroList
    }
    // 添加数据
    async create() {
        let user = this.ctx.request.body.params;
        console.log(user.countryId.id)
        console.log(user.student)
        await this.app.model.Hero.create({
            name: user.student.name,
            age: user.student.age,
            country_id: user.countryId
        })
        // let heroList = await this.app.model.Hero.findAll({});
        // return heroList
    }
    // 删除数据
    async destroy(){
        let id = this.ctx.params.id;
        await this.app.model.Hero.destroy({
            where:{
                id:id
            }
        })
        let heroList = await this.app.model.Hero.findAll({});
        return heroList
    }
    // 修改数据
    async update(){
        let user = this.ctx.request.body.student;
        await this.app.model.Hero.update({
            name: user.name,
            age: user.age,
            country_id: user.country_id
        },{
            where:{
                id: user.id
            }
        })
        let heroList = await this.app.model.Hero.findAll({});
        return heroList
    }
}

module.exports = HeroService;
